/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import entities.ContentEntity;
import entities.ContentStatisticEntity;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author vasi
 */
@Stateless
public class ContentEntityFacade implements ContentEntityFacadeLocal {
    @PersistenceContext(unitName = "AggregatorClient-ejbPU")
    private EntityManager em;

    public void create(ContentEntity contentEntity) {
        em.persist(contentEntity);
    }

    public void edit(ContentEntity contentEntity) {
        em.merge(contentEntity);
    }

    public void remove(ContentEntity contentEntity) {
        em.remove(em.merge(contentEntity));
    }

    public ContentEntity find(Object id) {
        return em.find(ContentEntity.class, id);
    }

    public List<ContentEntity> findAll() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(ContentEntity.class));
        return em.createQuery(cq).getResultList();
    }

    public List<ContentEntity> findRange(int[] range) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(ContentEntity.class));
        Query q = em.createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public List<ContentEntity> findRange(int[] range, String order_by, String order_type) {
        String query = "SELECT content FROM ContentEntity content, ContentStatisticEntity statistic " +
                "WHERE (content.cid = statistic.cid OR statistic.cid IS NULL)";
        if (order_by != null){
            if (order_by.equals("created")){
                query += " ORDER BY content.created";
            } else if (order_by.equals("rating")){
                query += " ORDER BY statistic.rating";
            } else if(order_by.equals("views")){
                query += " ORDER BY statistic.views";
            }
            if (order_type != null){
                if (order_type.equals("asc")){
                    query += " ASC";
                } else if(order_type.equals("desc")){
                    query += " DESC";
                }
            }
        }
        Query q = em.createQuery(query, ContentEntity.class);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        Root<ContentEntity> rt = cq.from(ContentEntity.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    public void generateTestEntries(){
        for (int index = 1; index<30; index++){
            ContentEntity con = new ContentEntity();
            con.setTitle("Contet title "+index);
            con.setBody("Content body "+index+"Content body "+index+"Content body "+index+"" +
                    "Content body "+index+"Content body "+index+"Content body "+index+"" +
                    "Content body "+index+"Content body "+index+"Content body "+index+"Content body "+index+"" +
                    "Content body "+index+"Content body "+index);
            con.setStatus(new Integer(1));
            con.setCreated(new Long(System.currentTimeMillis()/1000));
            em.persist(con);
        }
    }
}
